package com.airycode.distribution.distribution1.service;

import com.airycode.distribution.distribution1.domain.UserEntity;
import com.airycode.distribution.distribution1.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Created by zkr-fanxirui on 2017/8/15.
 */
@Service
public class UserService {
    @Autowired
    UserRepository userRepository;

    //添加方法
    public boolean addUser(UserEntity userEntity){
        boolean result = true;
        UserEntity entity = userRepository.saveAndFlush(userEntity);
        if (entity == null) {
            result = false;
        }

        return result;
    }

    //修改方法

    public boolean updateUser(UserEntity userEntity){
        boolean result = true;
        UserEntity entity = userRepository.saveAndFlush(userEntity);
        if (entity == null) {
            result = false;
        }

        return result;
    }

    //删除方法
    public boolean deleteUser(String id){
        boolean result = true;
        try{
            userRepository.delete(id);
        }catch (Exception e){
            result = false;
        }
        return result;
    }

    //查找方法
    public UserEntity getUserById(String id){
        UserEntity entity = userRepository.findOne(id);
        return entity;
    }

    //根据用户名和密码查找用户
    public UserEntity getUserByNameAndPassword(String name,String password){
        List<UserEntity> users = userRepository.findByUserNameAndUserPassword(name,password);
        if (users != null && users.size()>0) {
            return users.get(0);
        }
        return null;

    }




}
